/**
  ******************************************************************************
  * @file
  * @brief 液氮面电机控制
  * @author luffy
  * @version V1.0.0
  * @date 2022/12/14
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "func_pid.h"
#include "step_motor.h"
#include "AD7193.h"		
#include "FreeRTOS.h"
#include "func_pt100.h"
#include "lncup_ctl_task.h"
#include "pressuresensor.h"
#include "dev_adg1404.h"
#include "ParmManage.h"
#include "task.h"
#include "SYS.h"
#include "pfc_task.h"
/* Private defines -----------------------------------------------------------*/

/* Private Struct  -----------------------------------------------------------*/

/* Private enum    -----------------------------------------------------------*/

/* Private Variable  ---------------------------------------------------------*/
static int      s_lncup_state   = 0;          //液氮面控制状态
LNCTRL          s_LNCTRL;                     //液氮面控制参数
//Pointer

//Array

//Const

/*-----------------------------------------------------------------------------
                            内部函数声明
-----------------------------------------------------------------------------*/

/***********************************************************************************
 * @brief 液氮杯找零点函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void LNCUP_StateZero(void)
{
	float res1,res2,res3,res4,temp1 = 0;
	
	if(STEP_MOTOR_REVERSE_LIMIT != StepMotor.get_status(&StepMotor))
	{
		//电机先往零点限位外移动在返回停在零点限位处
////		StepMotor.set_step_dir(&StepMotor,Motor_Forward);  //方向待定
//		StepMotor.StepMotor_Set_Position_unit(&StepMotor, 0, s_LNCTRL.zeroForwardStep, RUN_CM); //根据距离正负控制电机运动方向
//		while(STEP_MOTOR_RUN == StepMotor.get_status(&StepMotor))
//		{
//			vTaskDelay(pdMS_TO_TICKS(10));
//		}
		
		vTaskDelay(pdMS_TO_TICKS(1000));
		
//		StepMotor.set_step_dir(&StepMotor,Motor_Reverse);  
		StepMotor.StepMotor_Set_Position_unit(&StepMotor, 0, s_LNCTRL.zeroReverseStep, RUN_CM);  //正：向上
		while(STEP_MOTOR_RUN == StepMotor.get_status(&StepMotor))
		{
			vTaskDelay(pdMS_TO_TICKS(10));
		}

	}
	
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(100));
		
		//test
		ADG1404_ON_CH(ADG1404_S1);                     //选择PT100检测通道
		res1 = adc7193.Read(&adc7193,AD7193_CH3,1); //
		ADG1404_ON_CH(ADG1404_S2);                     //选择PT100检测通道
		res2 = adc7193.Read(&adc7193,AD7193_CH2,1); //
		ADG1404_ON_CH(ADG1404_S3);   //选择PT100检测通道
		res3 = adc7193.Read(&adc7193,AD7193_CH0,1); //
		ADG1404_ON_CH(ADG1404_S4);   //选择PT100检测通道
		res4 = adc7193.Read(&adc7193,AD7193_CH1,1); //
		//res = adc7193.ReadTemp(&adc7193); //
		
		PT100_R2T(res1, &temp1);      //J53
		PT100_R2T(res2, &temp.en);    //J52  环境
		PT100_R2T(res3, &temp.ln);    //J50  液氮面
		PT100_R2T(res4, &temp.body);  //J51  基体腔
		// test
		
		if(s_LNCTRL.enable == 0)
			continue;
		else
		{
			s_lncup_state = LNCUP_STATE_RESET;
			return;
		}
	}
}

/***********************************************************************************
 * @brief 液氮杯复位函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void LNCUP_StateReset(void)
{
	//液氮面控制复位处
	StepMotor.set_step_dir(&StepMotor,Motor_Forward);  //方向待定
	StepMotor.StepMotor_Set_Position_unit(&StepMotor, 0, s_LNCTRL.resetStep, RUN_CM); //距离待定
	
	s_lncup_state = LNCUP_STATE_RUN;
	return;
}

/***********************************************************************************
 * @brief 液氮杯控制函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void LNCUP_StateRun(void)
{
	float step, res;
	
	while(1)
	{
		vTaskDelay(pdMS_TO_TICKS(s_LNCTRL.sampleCycle));
		
		ADG1404_ON_CH(ADG1404_S1);   //选择PT100检测通道
		res = adc7193.Read(&adc7193,AD7193_CH0,3); //
	
		//电压转电阻值res
		
		PT100_R2T(temp.ln, &res);
		
		if(STEP_MOTOR_REVERSE_LIMIT != StepMotor.get_status(&StepMotor) || 
			temp.ln < s_LNCTRL.targetTemp)
		{
			//温度转化电机步数step算法
		
			//StepMotor.set_step_dir(&StepMotor,Motor_Forward);  //方向待定
			StepMotor.StepMotor_Set_Position_unit(&StepMotor, 0, step, RUN_CM); //距离待定
		}
		
		if(s_LNCTRL.enable == 0)
		{
			s_lncup_state = LNCUP_STATE_STOP;
			return;
		}	
	}
}

/***********************************************************************************
 * @brief 液氮杯停止回位函数
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void LNCUP_StateStop(void)
{
	//液氮面控制复位处
	StepMotor.set_step_dir(&StepMotor,Motor_Reverse);  //方向待定
	StepMotor.StepMotor_Set_Position_unit(&StepMotor, 0, s_LNCTRL.stopStep, RUN_CM); //距离待定
	
	s_lncup_state = LNCUP_STATE_ZERO;
	return;
}

/*---------------------------------------------------------------------------------------
 函数原型: void LNCON_Thread(void *pvParameters)
 功    能: 液氮面控制线程执行函数
 输入参数: NA
 返 回 值: NA
 注意事项: 	
---------------------------------------------------------------------------------------*/
void LNCON_Thread(void *pvParameters)
{
	s_lncup_state = LNCUP_STATE_ZERO;
	
    while(1)
    {
		switch(s_lncup_state)
        {
			case LNCUP_STATE_ZERO:
                LNCUP_StateZero();
                break;
            case LNCUP_STATE_RESET:
                LNCUP_StateReset();
                break;
			case LNCUP_STATE_RUN:
				LNCUP_StateRun();
			  break;
            case LNCUP_STATE_STOP:
                LNCUP_StateStop();
                break;
        }
    }  
}

/***********************************************************************************
 * @brief 液氮面控制线程初始化
 * ex:
 * @par
 * None
 * @retval
 **********************************************************************************/
void LNCON_Init(void)
{
	s_LNCTRL.enable = 0;
	s_LNCTRL.sampleCycle = 3000;
	s_LNCTRL.targetTemp = -160;
	s_LNCTRL.resetStep = 100;
	s_LNCTRL.stopStep = 200;
	s_LNCTRL.zeroForwardStep = -10;
	s_LNCTRL.zeroReverseStep = 100;
	
	Dev_adc7193_Init(&adc7193);
}

/******************* (C) COPYRIGHT 2022 CIQTEK luffy *****END OF FILE****/
